package com.example.demo.sevice;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

@Service //一个服务类
public class MyAsyncService {
    @Resource
    JavaMailSenderImpl mailSender;//引入发送邮件的服务
    @Value("${spring.mail.username}")
    private String sendEmail;
    //发送邮件
    @Async //表示这个方法是异步运行的
    public void sendSms(String email,String subject,String text) throws InterruptedException {
        Long start = System.currentTimeMillis();//开始时间

//        Thread.sleep(4000);//模拟发送邮件
        SimpleMailMessage message = new SimpleMailMessage();
        message.setFrom(sendEmail);//发件人邮箱
        message.setTo(email);//收件人
        message.setSubject(subject);
        message.setText(text);
        mailSender.send(message);

        Long end = System.currentTimeMillis();//结束时间
        Long time = end - start;//总耗时
        System.out.println("这个异步任务耗时："+time);
    }

}
